package com.psp.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.psp.web.controller.res.BaseResult;
import com.psp.web.controller.res.ListResult;
import com.psp.web.controller.res.ObjectResult;
import com.psp.web.controller.res.bean.RAddressDetailBean;
import com.psp.web.controller.res.bean.RAddressListBean;
import com.psp.web.controller.res.bean.RAreaListBean;
import com.psp.web.controller.springmvc.req.GetAddressDeleteParam;
import com.psp.web.controller.springmvc.req.GetAddressDetailParam;
import com.psp.web.controller.springmvc.req.GetAddressListParam;
import com.psp.web.controller.springmvc.req.GetAddressSaveParam;
import com.psp.web.controller.springmvc.req.GetAddressSetDefaultParam;
import com.psp.web.controller.springmvc.req.GetAreaListParam;
import com.psp.web.service.AddressService;
import com.psp.web.service.exception.ServiceException;
import com.psp.web.service.impl.PageResult;

@Component
public class AddressController {

	Logger logger = Logger.getLogger(AddressController.class);

	@Autowired
	AddressService addressServiceImpl;

	public ListResult<RAddressListBean> list(GetAddressListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		String token = (String) request.getAttribute("token");
		String uid = (String) request.getAttribute("userId");
		int page = NumUtil.toInt(param.getPage(), 0);
		int pageSize = 10;
		int type = NumUtil.toInt(param.getType(), 0);// 地址类型：1收货地址，2发票管理

		ListResult<RAddressListBean> result = new ListResult<>();
		PageResult<RAddressListBean> resData = addressServiceImpl.getList(page, pageSize, uid, type);
		if (resData != null) {
			result.setTotalSize(resData.getCount());
			result.setData(resData.getData());
		}
		result.setFlag(true);
		result.setPage(page);
		result.setToken(token);
		return result;
	}

	public BaseResult save(GetAddressSaveParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String uid = (String) request.getAttribute("userId");
			String aid = param.getAid();
			String addressee = param.getAddressee();
			String phone = param.getPhone();
			String detail = param.getDetail();
			String pcds = param.getPcds();
			int type = NumUtil.toInt(param.getType(), 0);// 地址类型：1收货地址，2发票管理

			boolean flag = addressServiceImpl.save(uid, aid, addressee, phone, detail, pcds, type);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}

	public BaseResult delete(GetAddressDeleteParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String uid = (String) request.getAttribute("userId");
			String aid = param.getAid();
			boolean flag = addressServiceImpl.deleteById(uid, aid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}

	public BaseResult setDefault(GetAddressSetDefaultParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String uid = (String) request.getAttribute("userId");
			String aid = param.getAid();
			boolean flag = addressServiceImpl.setDefault(uid, aid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		result.setToken(token);
		return result;
	}

	public ObjectResult<RAddressDetailBean> getDetail(GetAddressDetailParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RAddressDetailBean> result = new ObjectResult<RAddressDetailBean>();
		String token = (String) request.getAttribute("token");
		String aid = param.getAid();
		RAddressDetailBean rBean = addressServiceImpl.getDetail(aid);
		result.setFlag(rBean != null);
		result.setData(rBean);
		result.setToken(token);
		return result;
	}

	public ListResult<RAreaListBean> getAreas(GetAreaListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		String id = param.getAdcode();
		ListResult<RAreaListBean> result = new ListResult<>();
		PageResult<RAreaListBean> resData = addressServiceImpl.getAreas(id);
		if (resData != null) {
			result.setTotalSize(resData.getCount());
			result.setData(resData.getData());
		}
		result.setFlag(true);
		return result;
	}

	public ListResult<RAreaListBean> getAllArea(HttpServletRequest request, HttpServletResponse response) {
		ListResult<RAreaListBean> result = new ListResult<>();
		PageResult<RAreaListBean> resData = addressServiceImpl.getAreas();
		if (resData != null) {
			result.setTotalSize(resData.getCount());
			result.setData(resData.getData());
		}
		result.setFlag(true);
		return result;
	}

}
